package com.sunyur.tailor.channel.service.inner.company.service.impl;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.company.param.PurchaserCompanyParam;
import com.sunyur.tailor.channel.service.inner.company.service.SunyurCompanyService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @program: tailor_channel
 * @description:
 * @author: pengbing
 * @create: 2021-06-01 17:20
 **/
@Service("SunyurCompanyService")
public class SunyurCompanyServiceImpl implements SunyurCompanyService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurCompanyServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurCompanyServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 创建公司主体 链接
     */
    @Value("${sunyur.api.createPurchaserCompany}")
    private String createPurchaserCompany;


    @Override
    public ChannelResult createPurchaserCompany(PurchaserCompanyParam param) throws Exception {
        LOGGER_INFO.info("PurchaserCompanyServiceImpl createPurchaserCompany param is {}", JSON.toJSONString(param));
        if (ObjectUtils.isEmpty(param)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "PurchaserCompanyServiceImpl createPurchaserCompany param is null");
        }
        // 调用主线接口
        SunyurResult sunyurResult = sunyurClient.execPost(createPurchaserCompany, JSON.toJSONString(param), null);
        // 主线报错
        if (!"success".equals(sunyurResult.getCode())) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

}
